package com.april.dcds;

/**
 * @author April
 * @param <E>
 * 栈的新建与入栈出站的实现
 */
public class LinkedStack<E> {
    private LinkNode<E> header;
    private int size=0;

    public LinkedStack(){
        this.header=null;
    }
    public LinkedStack(E e){
        this.header=new LinkNode<E>();
        this.header.value=e;
    }
    @Override
    public String toString() {
        String str="LinkedStack{";
        LinkNode l=this.header;
        for(int i=0;i<this.size-1;i++){
            str=str+l.value+',';
            l=l.next;
        }

        return str+l.value+'}';
    }





    public int size() {
        return size;
    }



    public void push(E e) {
        if(this.header==null){
            this.header=new LinkNode<E>();
            this.header.value=e;
        }else {
            LinkNode<E> node=new LinkNode<E>();
            node.value=e;
            node.next=this.header;
            this.header=node;
        }
        size++;
    }
    public E pop(){
        if(size==0){
            return null;
        }
        E e=this.header.value;
        this.header=this.header.next;
        size--;
        return e;
    }
}



